# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2010  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
sys.path.append('../')
sys.path.append('../common')

from PyQt4 import QtCore
from PyQt4.QtGui import *

from ui_toolsWidget import Ui_ToolsDockWidget
from ui_itemsWidget import Ui_ItemsDockWidget

from tools import *

import openbabel

class ToolWidget(QDockWidget):

	def __init__(self, scene, parent=None):
		QDockWidget.__init__(self, parent)

		self.ui = Ui_ToolsDockWidget()
		self.ui.setupUi(self)

		self.scene = scene
		self.scene.setTool(self.scene.draw)
		self.scene.setBond("single")

		self.toolButtonGroup = QButtonGroup()
		self.toolButtonGroup.addButton(self.ui.drawPushButton)
		self.toolButtonGroup.addButton(self.ui.movePushButton)
		self.toolButtonGroup.addButton(self.ui.rotatePushButton)
		self.toolButtonGroup.addButton(self.ui.erasePushButton)

		self.bondButtonGroup = QButtonGroup()
		self.bondButtonGroup.addButton(self.ui.singlePushButton)
		self.bondButtonGroup.addButton(self.ui.doublePushButton)
		self.bondButtonGroup.addButton(self.ui.triplePushButton)
		self.bondButtonGroup.addButton(self.ui.wedgePushButton)
		self.bondButtonGroup.addButton(self.ui.hashPushButton)
		self.bondButtonGroup.addButton(self.ui.squigglePushButton)
		self.bondButtonGroup.addButton(self.ui.cistransPushButton)

		self.connect(self.ui.drawPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setTool(self.scene.draw))
		self.connect(self.ui.movePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setTool(self.scene.move))
		self.connect(self.ui.rotatePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setTool(self.scene.rotate))
		self.connect(self.ui.erasePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setTool(self.scene.erase))
		self.connect(self.ui.singlePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("single"))
		self.connect(self.ui.doublePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("double"))
		self.connect(self.ui.triplePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("triple"))
		self.connect(self.ui.wedgePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("wedge"))
		self.connect(self.ui.hashPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("hash"))
		self.connect(self.ui.squigglePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("squiggle"))
		self.connect(self.ui.cistransPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setBond("cistrans"))
#		self.connect(self.ui.minimizePushButton, QtCore.SIGNAL("clicked()"), self.scene.compound.minimize)


class ItemWidget(QDockWidget):

	def __init__(self, scene, parent=None):
		QDockWidget.__init__(self, parent)

		self.ui = Ui_ItemsDockWidget()
		self.ui.setupUi(self)

		self.scene = scene

		self.buttonGroup = QButtonGroup()
		self.buttonGroup.addButton(self.ui.hydrogenPushButton)
		self.buttonGroup.addButton(self.ui.carbonPushButton)
		self.buttonGroup.addButton(self.ui.nitrogenPushButton)
		self.buttonGroup.addButton(self.ui.oxygenPushButton)
		self.buttonGroup.addButton(self.ui.phosphorusPushButton)
		self.buttonGroup.addButton(self.ui.sulfurPushButton)
		self.buttonGroup.addButton(self.ui.fluorinePushButton)
		self.buttonGroup.addButton(self.ui.chlorinePushButton)
		self.buttonGroup.addButton(self.ui.brominePushButton)
		self.buttonGroup.addButton(self.ui.iodinePushButton)
		self.buttonGroup.addButton(self.ui.ring3PushButton)
		self.buttonGroup.addButton(self.ui.ring4PushButton)
		self.buttonGroup.addButton(self.ui.ring5PushButton)
		self.buttonGroup.addButton(self.ui.ring6PushButton)
		self.buttonGroup.addButton(self.ui.ring7PushButton)
		self.buttonGroup.addButton(self.ui.ring8PushButton)
		self.buttonGroup.addButton(self.ui.ring5DoublePushButton)
		self.buttonGroup.addButton(self.ui.ring6DoublePushButton)

		self.makeItems()

		self.scene.setItem(self.carbon)

		self.connect(self.ui.hydrogenPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.hydrogen))
		self.connect(self.ui.carbonPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.carbon))
		self.connect(self.ui.nitrogenPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.nitrogen))
		self.connect(self.ui.oxygenPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.oxygen))
		self.connect(self.ui.phosphorusPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.phosphorus))
		self.connect(self.ui.sulfurPushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.sulfur))
		self.connect(self.ui.fluorinePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.fluorine))
		self.connect(self.ui.chlorinePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.chlorine))
		self.connect(self.ui.brominePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.bromine))
		self.connect(self.ui.iodinePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.iodine))

		self.connect(self.ui.ring3PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.ring3))
		self.connect(self.ui.ring4PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.ring4))
		self.connect(self.ui.ring5PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.ring5))
		self.connect(self.ui.ring6PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.ring6))
		self.connect(self.ui.ring7PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.ring7))
		self.connect(self.ui.ring8PushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.ring8))
		self.connect(self.ui.ring5DoublePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.ring5Double))
		self.connect(self.ui.ring6DoublePushButton, QtCore.SIGNAL("toggled(bool)"), lambda x: x and self.scene.setItem(self.ring6Double))

	def makeItems(self):
		self.hydrogen = openbabel.OBAtom()
		self.hydrogen.SetAtomicNum(1)
		self.carbon = openbabel.OBAtom()
		self.carbon.SetAtomicNum(6)
		self.nitrogen = openbabel.OBAtom()
		self.nitrogen.SetAtomicNum(7)
		self.oxygen = openbabel.OBAtom()
		self.oxygen.SetAtomicNum(8)
		self.phosphorus = openbabel.OBAtom()
		self.phosphorus.SetAtomicNum(15)
		self.sulfur = openbabel.OBAtom()
		self.sulfur.SetAtomicNum(16)
		self.fluorine = openbabel.OBAtom()
		self.fluorine.SetAtomicNum(9)
		self.chlorine = openbabel.OBAtom()
		self.chlorine.SetAtomicNum(17)
		self.bromine = openbabel.OBAtom()
		self.bromine.SetAtomicNum(35)
		self.iodine = openbabel.OBAtom()
		self.iodine.SetAtomicNum(53)

		self.ring3 = openbabel.OBMol()
		ra1=self.ring3.NewAtom()
		ra2=self.ring3.NewAtom()
		ra3=self.ring3.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		rb1=self.ring3.NewBond()
		rb2=self.ring3.NewBond()
		rb3=self.ring3.NewBond()
		rb1.SetBegin(ra1)
		rb1.SetEnd(ra2)
		rb2.SetBegin(ra2)
		rb2.SetEnd(ra3)
		rb3.SetBegin(ra3)
		rb3.SetEnd(ra1)
		rb1.SetBO(1)
		rb2.SetBO(1)
		rb3.SetBO(1)
		#builder = openbabel.OBOp.FindType("Gen3D")
		#builder.Do(self.ring3)
		ra1.SetVector(1,0,0)
		ra2.SetVector(0,-1,0)
		ra3.SetVector(0,1,0)
		#self.ring3.DeleteHydrogens()

		self.ring4 = openbabel.OBMol()
		ra1=self.ring4.NewAtom()
		ra2=self.ring4.NewAtom()
		ra3=self.ring4.NewAtom()
		ra4=self.ring4.NewAtom()
		ra1.SetAtomicNum(6)
		ra2.SetAtomicNum(6)
		ra3.SetAtomicNum(6)
		ra4.SetAtomicNum(6)
		rb1=self.ring4.NewBond()
		rb2=self.ring4.NewBond()
		rb3=self.ring4.NewBond()
		rb4=self.ring4.NewBond()
		rb1.SetBegin(ra1)
		rb1.SetEnd(ra2)
		rb2.SetBegin(ra2)
		rb2.SetEnd(ra3)
		rb3.SetBegin(ra3)
		rb3.SetEnd(ra4)
		rb4.SetBegin(ra4)
		rb4.SetEnd(ra1)
		rb1.SetBO(1)
		rb2.SetBO(1)
		rb3.SetBO(1)
		rb4.SetBO(1)
		ra1.SetVector(-.78,-.72,0)
		ra2.SetVector(-.72,.78,0)
		ra4.SetVector(.78,.72,0)
		ra3.SetVector(.72,-.78,0)

		
		self.ring5 = openbabel.OBMol()
		self.ring6 = openbabel.OBMol()
		self.ring7 = openbabel.OBMol()
		self.ring8 = openbabel.OBMol()
		self.ring5Double = openbabel.OBMol()
		self.ring6Double = openbabel.OBMol()
		
